package com.atwisdom.star.dcs.util;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateUtils {

    public static LocalDate ConvertDateToLocalDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime.toLocalDate();
    }


    public static LocalDateTime ConvertDateToLocalDateTime(Date date){
        Instant instant = date.toInstant();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        return localDateTime;
    }



    public static Date ConvertLocalDateToDate(LocalDate localDate) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
        return Date.from(instant);
    }

    public static DateTime LocalDateTimeToHutoolDateTime(LocalDateTime localDateTime) {
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
        // 将ZonedDateTime转换为Instant，再转换为java.util.Date
        Date date = Date.from(zonedDateTime.toInstant());
        // 使用Hutool的DateUtil将Date转换为DateTime
        return DateUtil.date(date);
    }


    public static LocalDateTime hutoolDateTimeToLocalDateTime(DateTime dateTime) {
        // 将DateTime转换为java.util.Date（实际上不需要转换，因为DateTime继承自Date）
        Date date = dateTime;
        // 将Date转换为Instant
        Instant instant = date.toInstant();
        // 将Instant转换为LocalDateTime
        return instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
    }


    public static String getLocalDateFormat(LocalDate localDate, String pattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return localDate.format(dateTimeFormatter);
    }

    public static String getLocalDateFormat(LocalDateTime localDateTime, String pattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(dateTimeFormatter);
    }

    public static String getDateFormat(Date date, String pattern) {
        if(date==null){
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static LocalDateTime getLocalDateTime(LocalDate date,String timeStr) {
        String[] timeStrAry = timeStr.split(":");
        if(timeStrAry[0].length() == 1){
            timeStr = "0"+timeStr;
        }
        LocalTime time = LocalTime.parse(timeStr); // 解析时间字符串
        return LocalDateTime.of(date, time);
    }

    /**
     * 获取日期加上一个小时数（double）的时间
     * @param dateTime
     * @param hours
     * @return
     */
    public static LocalDateTime getLocalDateTimePlusHours(LocalDateTime dateTime,Double hours) {
        int wholeHours = (int)Math.floor(hours);  // 将小时数转换为整数部分
        int minutes = (int) ((hours - wholeHours) * 60);  // 将小数部分转换为分钟数
        LocalDateTime resultDateTime = dateTime.plusHours(wholeHours).plusMinutes(minutes);
        return resultDateTime;
    }

    /**
     * 获取日期减去一个小时数（double）的时间
     * @param dateTime 时间 年月日时分秒
     * @param hours 小时
     * @return 时间
     */
    public static LocalDateTime getLocalDateTimeMinusHours(LocalDateTime dateTime,Double hours) {
        int wholeHours = (int)Math.floor(hours);  // 将小时数转换为整数部分
        int minutes = (int) Math.ceil((hours - wholeHours) * 60);  // 将小数部分转换为分钟数
        LocalDateTime resultDateTime = dateTime.plusHours(-wholeHours).plusMinutes(-minutes);
        return resultDateTime;
    }

    /**
     * 获取当月第一天
     * @param year
     * @param month
     * @return
     */
    public static String getStartOfMonth(int year, int month) {
        // 计算当月的第一天 yyyy-MM-dd
        DateTime parseDateTime = DateUtil.parse(year + "-" + month + "-01", "yyyy-MM-dd");
        String firstDayOfMonth = DateUtil.beginOfMonth(parseDateTime).toString();
        System.out.println("当月的第一天：" + firstDayOfMonth);
        return firstDayOfMonth;
    }

    /**
     * 获取当月最后一天
     * @param year
     * @param month
     * @return
     */
    public static String getEndOfMonth(int year, int month) {
        // 计算当月的最后一天 yyyy-MM-dd
        DateTime parseDateTime = DateUtil.parse(year + "-" + month + "-01", "yyyy-MM-dd");
        String lastDayOfMonth = DateUtil.endOfMonth(parseDateTime).toString();
        System.out.println("当月的最后一天：" + lastDayOfMonth);
        return lastDayOfMonth;
    }

    public static long getMinuteTimestamp(long timestamp) {
        return  (timestamp / (60 * 1000)) * (60 * 1000);  // 将时间戳取整到分钟数
    }

    /**
     *  获取月的第一天时间戳
     * @param timestamp
     * @return
     */
    public static long getMonthFirstTimestamp(long timestamp) {
        Date date = new Date(timestamp);
        LocalDate localDate = DateUtils.ConvertDateToLocalDate(date);
        Date firstDate = DateUtils.ConvertLocalDateToDate(localDate.withDayOfMonth(1));
        return firstDate.getTime();
    }

    /**
     *  获取月的最后一天时间戳
     * @param timestamp
     * @return
     */
    public static long getMonthLastTimestamp(long timestamp) {
        Date date = new Date(timestamp);
        LocalDate localDate = DateUtils.ConvertDateToLocalDate(date);
        LocalDate lastLocalDate = localDate.withDayOfMonth(1).plusMonths(1).plusDays(-1);
        Date lastDate = DateUtils.ConvertLocalDateToDate(lastLocalDate);
        return lastDate.getTime();
    }


    /**
     *  获取月的第一天时间戳
     * @param timestamp
     * @return
     */
    public static long getYearFirstTimestamp(long timestamp) {
        Date date = new Date(timestamp);
        LocalDate localDate = DateUtils.ConvertDateToLocalDate(date);
        Date firstDate = DateUtils.ConvertLocalDateToDate(localDate.withMonth(1).withDayOfMonth(1));
        return firstDate.getTime();
    }

    /**
     *  获取月的最后一天时间戳
     * @param timestamp
     * @return
     */
    public static long getYearLastTimestamp(long timestamp) {
        Date date = new Date(timestamp);
        LocalDate localDate = DateUtils.ConvertDateToLocalDate(date);
        LocalDate lastLocalDate = localDate.withMonth(1).withDayOfMonth(1).plusYears(1).plusDays(-1);
        Date lastDate = DateUtils.ConvertLocalDateToDate(lastLocalDate);
        return lastDate.getTime();
    }

}
